package study.itheima;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class PracticeServlet1 extends HttpServlet {
//    生命周期
    @Override
    public void destroy() {
        System.out.println("destroy执行了");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("init方法执行了");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("get方法执行了");
//        商品打折
        /*String uri = req.getRequestURI();
        uri = uri.substring(uri.lastIndexOf("/"));
        if("/vip".equals(uri)){
            System.out.println("vip九折");
        }else if("/vvip".equals(uri)){
            System.out.println("vvip五折");
        }else {
            System.out.println("原价");
        }*/
//        congfig四个方法
        ServletConfig config = getServletConfig();
        String encoding = config.getInitParameter("encoding");
        System.out.println("encoding = " + encoding);
        System.out.println("------------");
        Enumeration<String> names = config.getInitParameterNames();
        while (names.hasMoreElements()){
            String s = names.nextElement();
            System.out.println(s + "," + config.getInitParameter(s));
        }
        System.out.println("--------------");
        System.out.println(config.getServletName());
        System.out.println("--------------");
        ServletContext context = config.getServletContext();
        System.out.println(context);
        System.out.println("=================");
//        context方法
        context.setAttribute("天王盖地虎","宝塔镇河妖");
        String path1 = context.getContextPath();
        System.out.println(path1);
        System.out.println("--------------");
        System.out.println(context.getRealPath("/"));
        System.out.println(context.getRealPath("/c.txt"));
        System.out.println(context.getRealPath("/WEB-INF/b.txt"));
        System.out.println(context.getRealPath("/WEB-INF/classes/a.txt"));
        System.out.println(context.getServerInfo());
        String sss = context.getInitParameter("contextname");
        System.out.println(sss);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doGet(req, resp);
        System.out.println("dopost方法执行了");
    }
}
